package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.Contract;
import com.sl.au.entity.ContractPart;
import com.sl.au.entity.ContractPump;
import com.sl.au.entity.ContractRepair;
import com.sl.au.entity.Module;
import com.sl.au.entity.Option;
import com.sl.au.entity.ScadaBasePointScadaRtuBase;
import com.sl.au.entity.ScadaRtuBaseInfo;
import com.sl.au.entity.User;
import com.sl.au.entity.scadaModelBaseInfo;
import com.sl.au.service.ExdeScadaModelBaseInfoService;

@RestController
@RequestMapping(value = "/ui/ScadaModelBaseInfo/")
public class ExdeScadaModelBaseInfoController extends BaseController {
	
	@Autowired
	ExdeScadaModelBaseInfoService exdeScadaModelBaseInfoService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		Module module = this.getPageModule("scada");
		ModelAndView modelAndView = this.createLayoutView("scada/scadamodelbaseInfoindex");
		modelAndView.addObject("module", module);
		
		return modelAndView;
	}
	
	/**
	 * 获取页面数据
	 */
	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> condition = new HashMap<String, String>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		Account a = this.getMyAccount();
		Module module = this.getPageModule("scadamodelbaseInfoindex");
		String dataFilter = module.getDataFilter();
		User user = a.getUser();
		//this.getQueryParams(qMap,numMap);
		
		String userId = a.getUser().getId();
		String Superior = a.getUser().getSaleArea().getSuperior();//权限部门
		String areaName = a.getUser().getSaleArea().getAreaName();
		if (key == null) {
			key = "";
		}
		if (userId == null) {
			userId = "";
		}
		if (Superior == null) {
			Superior = "";
		}
		if (areaName == null) {
			areaName = "";
		}
		if (dataFilter == null) {
			dataFilter = "";
		}
		condition.put("key", key);
		condition.put("userId", userId);
		condition.put("superior", Superior);
		condition.put("areaName", areaName);
		condition.put("dataFilter", dataFilter);
		List<scadaModelBaseInfo> scadaRtuBaseInfos = exdeScadaModelBaseInfoService.getscadaModelBaseInfos(start, length, condition, user, "createTime", "desc");
		long count = exdeScadaModelBaseInfoService.getscadaModelBaseInfosCount(0, Integer.MAX_VALUE, condition, user, "createTime", "desc");
		map.put("data", scadaRtuBaseInfos);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public ModelAndView ScadaModelBaseInfoAdd(HttpServletRequest request) {
		Account a = this.getMyAccount();
		ModelAndView modelAndView = this.createLayoutView("scada/scadamodelbaseInfo-add");
		modelAndView.addObject("noteId", "");
		return modelAndView;
	}
	
	@RequestMapping(value = "edit", method = RequestMethod.GET)
	public ModelAndView ScadaModelBaseInfoEdit(HttpServletRequest request) {
		Account a = this.getMyAccount();
		ModelAndView modelAndView = this.createLayoutView("scada/scadamodelbaseInfo-edit");
		String noteId = request.getParameter("id");
		modelAndView.addObject("noteId", noteId);
		return modelAndView;
	}
	
	/**
	 * 保存节点
	 * @param scadamodelBaseInfo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "saveScadaModelBaseInfo", method = RequestMethod.POST)
	public Map<String, Object> saveScadaModelBaseInfo(@RequestBody scadaModelBaseInfo scadamodelBaseInfo, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Account a = this.getMyAccount();
		if(scadamodelBaseInfo.getId()==null||scadamodelBaseInfo.getId().equals("")){
			scadamodelBaseInfo.setId(java.util.UUID.randomUUID().toString());
		}
		scadaModelBaseInfo old = exdeScadaModelBaseInfoService.getExdeScadaModelBaseInfoRepository().findOne(scadamodelBaseInfo.getId());
		if(null!=old){
			scadamodelBaseInfo.setUpParaId(old.getUpParaId());
			scadamodelBaseInfo.setCreateTime(old.getCreateTime());
		}
		
		if(scadamodelBaseInfo.getUpParaId()==null || scadamodelBaseInfo.getUpParaId().equals("")){
			scadamodelBaseInfo.setUpParaId(null);
		}
		User user = a.getUser();
		scadamodelBaseInfo.setUser(user);
		exdeScadaModelBaseInfoService.getExdeScadaModelBaseInfoRepository().save(scadamodelBaseInfo);
		if(scadamodelBaseInfo.getUpParaId()==null){
			map.put("noteId", scadamodelBaseInfo.getId());
		}
		return map;
	}
	/**
	 * 点位引入
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "savescadaModelPointRelation", method = RequestMethod.GET)
	public Map<String, Object> savescadaModelPointRelation( HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Account a = this.getMyAccount();
		User user = a.getUser();
		String modelId = request.getParameter("modelId");//节点id
		String pointIds = request.getParameter("pointIds");//点位ids
		String[] point = pointIds.split(",");
		exdeScadaModelBaseInfoService.savescadaModelPointRelation(modelId, point, user);
		return map;
	}
	//引入模型
	@RequestMapping(value = "savescadaModelPoint", method = RequestMethod.GET)
	public Map<String, Object> savescadaModelPoint( HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Account a = this.getMyAccount();
		User user = a.getUser();
		String modelId = request.getParameter("modelId");//父节点id
		String pointId = request.getParameter("pointId");//模型根节点id
		exdeScadaModelBaseInfoService.savescadaModelPoint(modelId, pointId, user);
		map.put("code", 0);
		return map;
	}
	/**
	 * 点位引入删除
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "deletePoint", method = RequestMethod.GET)
	public Map<String, Object> deletePoint(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String pointIds = request.getParameter("pointIds");
		if (StringUtils.hasText(pointIds)) {
			Account a = this.getMyAccount();
			User user = a.getUser();
			String[] pointId = pointIds.split(",");
			pointIds = "";
			for(int i=0;i<pointId.length;i++){
				pointIds += "'" + pointId[i] + "',";
			}
			pointIds = pointIds.substring(0, pointIds.length()-1);
			exdeScadaModelBaseInfoService.deletepoint(pointIds);
		}
		map.put("code", 0);
		return map;
	}
	/**
	 * 删除节点
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "deleteNote", method = RequestMethod.GET)
	public Map<String, Object> deleteNote(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if (StringUtils.hasText(id)) {
			Account a = this.getMyAccount();
			User user = a.getUser();
			exdeScadaModelBaseInfoService.deleteNote(id);
		}
		map.put("code", 0);
		return map;
	}
	
	/**
	 * /得到树节点数据
	 */

	@RequestMapping(value = "getTree", method = RequestMethod.GET)
	public Map<String, Object> getTree() {
		Map<String, Object> map = new HashMap<String, Object>();
		//String deviceType="产品类型";
		//List<scadaModelBaseInfo> scadaModelBaseInfos=new ArrayList<>();
		String id = request.getParameter("id");
		List<Map<String,Object>> deviceTypeMap=new ArrayList<Map<String,Object>>();
		if(id!=null){
			deviceTypeMap=exdeScadaModelBaseInfoService.getNodeMap(id);
		}
		//scadaModelBaseInfos = exdeScadaModelBaseInfoService.getExdeScadaModelBaseInfoRepository().findupParaId();
		map.put("code", 1);
		map.put("deviceType", deviceTypeMap);
		return map;
	}
	/**
	 * 引入和未引入点位列表加载
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getpageScadaBase", method = RequestMethod.GET)
	public Map<String, Object> getpageScadaBase(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> condition = new HashMap<String, String>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");//查询条件
		String Type = request.getParameter("Type");//引入或未引入
		String pointType = request.getParameter("pointType");//实点或虚点
		String id = request.getParameter("id");//节点id，加载引入列表
		Account a = this.getMyAccount();
		Module module = this.getPageModule("scadamodelbaseInfoindex");
		String dataFilter = module.getDataFilter();
		User user = a.getUser();
		//this.getQueryParams(qMap,numMap);
		
		String userId = a.getUser().getId();
		String Superior = a.getUser().getSaleArea().getSuperior();//权限部门
		String areaName = a.getUser().getSaleArea().getAreaName();
		if (key == null) {
			key = "";
		}
		if (id == null) {
			id = "";
		}
		if (Type == null) {
			Type = "";
		}
		if (pointType == null) {
			pointType = "";
		}
		if (userId == null) {
			userId = "";
		}
		if (Superior == null) {
			Superior = "";
		}
		if (areaName == null) {
			areaName = "";
		}
		if (dataFilter == null) {
			dataFilter = "";
		}
		condition.put("key", key);
		condition.put("id", id);
		condition.put("Type", Type);
		condition.put("pointType", pointType);
		condition.put("userId", userId);
		condition.put("superior", Superior);
		condition.put("areaName", areaName);
		condition.put("dataFilter", dataFilter);
		List<ScadaBasePointScadaRtuBase> scadaRtuBaseInfos = exdeScadaModelBaseInfoService.getScadaBasePointScadaRtuBases(start, length, condition, user, "createTime", "desc");
		long count = exdeScadaModelBaseInfoService.getScadaBasePointScadaRtuBases(0, Integer.MAX_VALUE, condition, user, "createTime", "desc").size();
		map.put("data", scadaRtuBaseInfos);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
}
